<template>
  <div class="main">
    <el-dialog title="工作成果提交" width="40%" :visible.sync="dialogFormVisible">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="80px"
        class="demo-ruleForm"
      >
        <el-form-item label="实际工时" prop="useHour">
          <el-input
            v-model.number="ruleForm.useHour"
            maxlength="5"
            style="width:50%"
            placeholder="请输入实际投入工时"
          ></el-input>小时
        </el-form-item>
        <el-form-item label="当前进度" prop="progress">
          <template>
            <div class="block">
              <el-slider
                v-model="ruleForm.progress"
                show-input>
              </el-slider>
            </div>
            <div class="tip">该任务截至当日的完成进度百分比</div>
          </template>
        </el-form-item>
        <el-form-item label="工作描述" prop="workNotes">
          <el-input
            type="textarea"
            v-model="ruleForm.workNotes"
            maxlength="200"
            rows="3"
            show-word-limit
          ></el-input>
        </el-form-item>
        <!-- -->
        <el-form-item label="附件">
          <FileUpload :limit="5" :fileSize="10" v-model="attachments">
          </FileUpload>
        </el-form-item>
        <!-- <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >立即创建</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeForm">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { executeWork } from "@/api/system/work";
export default {
  name: "addform",
  data() {
    return {
      dialogFormVisible: false,
      formLabelWidth: "120px",
      ruleForm: { progress:0 },
      rules: {
        useHour: [
          { required: true, message: "请输入今日实际工时", trigger: "blur" },
          { type: "number", message: "实际工时必须为数字值" }
        ],
        progress: [
          { required: true, message: "请输入当前进度", trigger: "blur" },
          { type: "number", message: "当前进度必须为数字值" }
        ],
        workNotes:[{ required: true, message: "请输入当日工作描述", trigger: "blur" },]
      },
      attachments:[],
    };
  },
  created() {
  },
  methods: {
    open() {
      this.dialogFormVisible = true;
    },
    closeForm() {
      this.resetForm("ruleForm");
      this.dialogFormVisible = false;
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          // this.ruleForm.temp = this.ruleForm.useHour;
          // // console.log("temphour", this.ruleForm.temp);
          // this.edithourUnit();
          //文件处理
          if(this.attachments && this.attachments.length>0){
            this.ruleForm.files=[];
            this.attachments.forEach(f=>{
              this.ruleForm.files.push({fileName: f.name, filePath: f.url, fileSize: f.size});
            });
          }
          executeWork(this.ruleForm)
            .then(res => {
              // console.log(res);
              if (res.code == 200) {
                this.resetForm(formName);
                this.dialogFormVisible = false;
                this.$parent.init();
              }
            })
            .catch(error => {
              //  if (res.code == 500) {
              // this.ruleForm.useHour = this.ruleForm.temp;
              // this.$message.warning("工时还原为" + this.ruleForm.temp);
              // console.log("temphour", this.ruleForm.temp);
              // }
              // console.log(error);
            });
            
        } else {
          // console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  }
};
</script>

<style scoped lang="scss">
.tip{
  font-size: 12px;
    color: #606266;
    margin-top: 7px;
}
.col2-5 {
    width: 300px;
    display: flex;
    align-items: center;
    justify-content: space-around;
    > div {
      width: 280px;
      cursor: pointer;
      user-select: none;
    }
  }
  .el-progress-bar__innerText{
    color: #000000;
  }
</style>
